package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.EducationBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PopulationMoveSearchBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MovePopulationSearchActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.et_Name)
    EditText et_Name;
    @BindView(R.id.et_IDCardNum) EditText et_IDCardNum;
    @BindView(R.id.et_Address) EditText et_Address;
    @BindView(R.id.sp_Cancel) Spinner sp_Cancel;//注销状态
    @BindView(R.id.sp_Death) Spinner sp_Death;
    @BindView(R.id.sp_Sex) Spinner sp_Sex;
    @BindView(R.id.sp_Nation) Spinner sp_Nation;//民族
    @BindView(R.id.sp_Marriage) Spinner sp_Marriage;
    @BindView(R.id.sp_PersonType) Spinner sp_PersonType;//政治面貌
    @BindView(R.id.tv_Education) TextView tv_Education;//学历
    @BindView(R.id.sp_InReason) Spinner sp_InReason;//流入原因
    @BindView(R.id.sp_Fixed) Spinner sp_Fixed;//转常住人口
    @BindView(R.id.tv_RegStartTime) TextView tv_RegStartTime;//登记日期
    @BindView(R.id.tv_RegEndTime) TextView tv_RegEndTime;
    @BindView(R.id.tv_CredStartTime) TextView tv_CredStartTime;//证件日期
    @BindView(R.id.tv_CredEndTime) TextView tv_CredEndTime;
    @BindView(R.id.tv_BirthStartTime) TextView tv_BirthStartTime;//出生日期
    @BindView(R.id.tv_BirthEndTime) TextView tv_BirthEndTime;
    private List<GlobalBean> sexState,nationState, marriageState,personType,fixedList;
    private EducationBean.SchoolingsBean schoolingsBean;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_move_population_search);
        setTitleName("流动人口查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        PublicListPest pubList=new PublicListPest(this);
        pubList.getPublicList("性别");
        pubList.getPublicList("民族");
        pubList.getPublicList("婚姻状况");
        pubList.getPublicList("政治面貌");
        pubList.getPublicList("流入原因");
    }


    @OnClick({R.id.tv_right,R.id.tv_Education,R.id.tv_RegStartTime,R.id.tv_RegEndTime,R.id.tv_CredStartTime,
            R.id.tv_CredEndTime,R.id.tv_BirthStartTime,R.id.tv_BirthEndTime,})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
              submit();
                break;
            case R.id.tv_Education:
                startActivityForResult(new Intent(this,EducationActivity.class),4447);
                break;
            case R.id.tv_RegStartTime:
                BaseUtil.createDataPop(this,tv_RegStartTime);
                break;
            case R.id.tv_RegEndTime:
                BaseUtil.createDataPop(this,tv_RegEndTime);
                break;
            case R.id.tv_CredStartTime:
                BaseUtil.createDataPop(this,tv_CredStartTime);
                break;
            case R.id.tv_CredEndTime:
                BaseUtil.createDataPop(this,tv_CredEndTime);
                break;
            case R.id.tv_BirthStartTime:
                BaseUtil.createDataPop(this,tv_BirthStartTime);
                break;
            case R.id.tv_BirthEndTime:
                BaseUtil.createDataPop(this,tv_BirthEndTime);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 4447://学历
                    schoolingsBean= ( EducationBean.SchoolingsBean)
                            data.getSerializableExtra("SchoolingsBean");
                    tv_Education.setText(schoolingsBean.getName());
                    break;
            }
        }
    }

    private void submit() {
        try {
            String name=et_Name.getText().toString().trim();
            String idCardNum=et_IDCardNum.getText().toString().trim();
            String address=et_Address.getText().toString().trim();
            int cancelState=sp_Cancel.getSelectedItemPosition()==0?0:1;
            int deathState=sp_Death.getSelectedItemPosition()==0?0:1;
            GlobalBean sexBean=(GlobalBean)sp_Sex.getSelectedItem();
            GlobalBean nBean=(GlobalBean)sp_Nation.getSelectedItem();
            GlobalBean mBean=(GlobalBean)sp_Marriage.getSelectedItem();
            GlobalBean perBean=(GlobalBean)sp_PersonType.getSelectedItem();
            String schoolings="";
            if (schoolingsBean!=null){
                schoolings=schoolingsBean.getValue();
            }

            GlobalBean iRBean=(GlobalBean)sp_InReason.getSelectedItem();
            int fixed=sp_Fixed.getSelectedItemPosition()==0?0:1;
            String regStartTime=tv_RegStartTime.getText().toString();
            String regEndTime=tv_RegEndTime.getText().toString();
            String credStartTime=tv_CredStartTime.getText().toString();
            String credEndTime=tv_CredEndTime.getText().toString();
            String birthStartTime=tv_BirthStartTime.getText().toString();
            String birthEndTime=tv_BirthEndTime.getText().toString();
            PopulationMoveSearchBean searchBean=new PopulationMoveSearchBean();
            searchBean.setName(name);
            searchBean.setIdCardNo(idCardNum);
            searchBean.setCurrentAddress(address);
            searchBean.setLogOut(cancelState);
            searchBean.setIsDeath(deathState);
            if (sexBean!=null){
                String sexId=(sexBean.getId()).equals("-1") ?"":sexBean.getId();
                searchBean.setGender(sexId);
            }
            if (nBean!=null){
                String nationId=(nBean.getId()).equals("-1") ?"":nBean.getId();
                searchBean.setNation(nationId);
            }
            if (mBean!=null){
                String marriageId=(mBean.getId()).equals("-1") ?"":mBean.getId();
                searchBean.setMaritalState(marriageId);
            }
            if (perBean!=null){
                String perTypeId=(perBean.getId()).equals("-1") ?"":perBean.getId();
                searchBean.setPoliticalBackground(perTypeId);
            }
            searchBean.setSchoolStr(schoolings);
            if (iRBean!=null){
                String inResaon=(iRBean.getId()).equals("-1") ?"":iRBean.getId();
                searchBean.setReason(inResaon);
            }
            searchBean.setFixed(fixed);
            searchBean.setRegStartTime(regStartTime);
            searchBean.setRegEndTime(regEndTime);
            searchBean.setCredStartTime(credStartTime);
            searchBean.setCredEndTime(credEndTime);
            searchBean.setBirthday(birthStartTime);
            searchBean.setEndBirthday(birthEndTime);
            MyRxBus.get().post(new MsgEvent<>("MovePopulationActivity",1,searchBean));
            finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("性别".equals(publicBean.getDomainName())){
            sexState=publicBean.getDicts();
            sexState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Sex,sexState);
        }
        if ("民族".equals(publicBean.getDomainName())){
            nationState=publicBean.getDicts();
            nationState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Nation,nationState);
        }
        if ("婚姻状况".equals(publicBean.getDomainName())){
            marriageState=publicBean.getDicts();
            marriageState.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Marriage,marriageState);
        }
        if ("政治面貌".equals(publicBean.getDomainName())){
            personType=publicBean.getDicts();
            personType.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_PersonType,personType);
        }
        if ("流入原因".equals(publicBean.getDomainName())){
            fixedList=publicBean.getDicts();
            fixedList.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_InReason,fixedList);
        }
    }

    private void setSpinnerAdapter(Spinner spinner, List<GlobalBean> lists) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,lists);
        spinner.setAdapter(adapter);
    }
}
